package com.meiqiongproperty.yiguanjia.fragment.home;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.commom.ViewConstant;
import com.meiqiongproperty.yiguanjia.databinding.HomeIndexLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.health.YiHealthFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.life.YiLifeFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.shopping.YiShopFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.tenement.TenementFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.yipension.YiPensionFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendAdvertId;
import com.meiqiongproperty.yiguanjia.httpbean.AdvertInfoBean;
import com.meiqiongproperty.yiguanjia.ui.adbanner.animations.DescriptionAnimation;
import com.meiqiongproperty.yiguanjia.ui.adbanner.indicators.PagerIndicator;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.BaseSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.TextSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.viewpagerswitchanimation.SliderLayout;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment
 * @UserChenJing
 * @Date2017/4/22
 */

public class HomeIndexFragment extends BaseFragment {
    private HomeIndexLayoutBinding binding;
    Handler handler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.home_index_layout, container, false);
        binding.setClick(this);
        binding.slider.setBottomIndicator(View.GONE);
        binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);

        getAdvertInfo();

        String[] strings = new String[ViewConstant.notice.size()];
        final String[] strs = ViewConstant.notice.toArray(strings);
        binding.marqueeview.setTextArray(strs);
        binding.marqueeview.setReRepeatDelayTime(2000);
        binding.marqueeview.setItemAnimationTime(2000);
        return binding.getRoot();
    }


    public void toTenMentFragment(View view) {
        GenericFragmentActivity.start(getActivity(), TenementFragment.class, new Bundle());
    }

    public void shoppingClick(View view) {
        GenericFragmentActivity.start(getActivity(), YiShopFragment.class, new Bundle());
    }

    public void yiLiftClick(View view) {
        GenericFragmentActivity.start(getActivity(), YiLifeFragment.class, new Bundle());
    }

    public void yiPensionClick(View view) {
        GenericFragmentActivity.start(getActivity(), YiPensionFragment.class, new Bundle());
    }

    public void yiHealthyMedicalClick(View view) {
        GenericFragmentActivity.start(getActivity(), YiHealthFragment.class, new Bundle());
    }

    public void yiFinacialClick(View view) {
        GenericFragmentActivity.start(getActivity(), YiFinancialFragment.class, new Bundle());
    }

    @Override
    public void onResume() {
        super.onResume();
        sendPost();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.marqueeview.resume();
            }
        }, 3000);
    }

    @Override
    public void onStop() {
        super.onStop();
        binding.marqueeview.pause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding.marqueeview.destory();
    }


    //获取广告
    public void getAdvertInfo() {
        SendAdvertId send = new SendAdvertId();
        send.setAdvertId("1");
        send.setToken(Utils.getToken(getActivity()));
        ApiService.getAdvertInfo(getActivity(), new IHttpCallback<AdvertInfoBean>() {
            @Override
            public void onNext(final AdvertInfoBean advertInfoBean) {
                initAdv(advertInfoBean);
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(send));
    }

    public void initAdv(final AdvertInfoBean advertInfoBean) {
        binding.slider.getPagerIndicator().setDefaultIndicatorColor(getResources().getColor(R.color.transparent), Color.argb(1, 255, 255, 255));

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.slider.stopAutoCycle();
            }
        }, 200);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (advertInfoBean.getAdvert().size() <= 1) {
                    binding.slider.setBottomIndicator(View.GONE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
                } else {
                    binding.slider.setBottomIndicator(View.VISIBLE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Visible);
                    binding.slider.getPagerIndicator().setDefaultIndicatorColor(getResources().getColor(R.color.header_color), Color.argb(66, 255, 255, 255));
                }

                if (advertInfoBean.getAdvert().size() <= 1) {
                    binding.slider.stopAutoCycle();
                } else {
                    binding.slider.startAutoCycle();
                }
            }
        }, advertInfoBean.getAdvert().size()*1000);

        List<String> img = new ArrayList<String>();
        List<String> imgurl = new ArrayList<String>();
        List<String> title = new ArrayList<String>();
        for (int i = 0; i < advertInfoBean.getAdvert().size(); i++) {
            img.add(i, advertInfoBean.getAdvert().get(i).getImage());
            imgurl.add(i, advertInfoBean.getAdvert().get(i).getUrl());
            title.add(i, advertInfoBean.getAdvert().get(i).getTitle());
        }


        for (int i = 0; i < advertInfoBean.getAdvert().size(); i++) {
            TextSliderView textSliderView = new TextSliderView(getActivity(), img, imgurl, title);
            // initialize a SliderLayout
            textSliderView
                    .image(img.get(i))
                    .setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                        @Override
                        public void onSliderClick(BaseSliderView slider) {

                        }
                    });

            binding.slider.addSlider(textSliderView);
        }
        binding.slider.setPresetTransformer(SliderLayout.Transformer.Stack);
        binding.slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        binding.slider.setCustomAnimation(new DescriptionAnimation());
    }

    public void sendPost() {
        JsonObjectRequest order = new JsonObjectRequest(com.android.volley.Request.Method.POST,
                "http://114.215.191.184:8280/seiko-wap/version/test", null,
                new com.android.volley.Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject arg0) {
                        LogUtil.i("expay===========>" + arg0);
                        try {
                            if (arg0.getInt("data") != 1) {
                                binding.setClick(null);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {

            }
        });
        order.setRetryPolicy(new DefaultRetryPolicy(20 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(order);
    }
}
